#!/bin/bash
#description: install php-memcached extension
#define variables
PHP_MEMCACHED_VERSION=memcached-3.0.3
SOFTDIR=/usr/local/src
[ ! -d $SOFTDIR ] && mkdir $SOFTDIR
#install libmemcached(memcached扩展是基于libmemcached)

cd $SOFTDIR 
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz 
[ ! -f libmemcached-1.0.18 ] && tar xf libmemcached-1.0.18.tar.gz && cd libmemcached-1.0.18
./configure --prefix=/usr/local/libmemcached  --with-memcached  && make && make install
[ $? -ne 0 ] && exit 1

#install memcached extension

cd $SOFTDIR 
wget http://pecl.php.net/get/${PHP_MEMCACHED_VERSION}.tgz 
[ ! -f ${PHP_MEMCACHED_VERSION} ] && tar xf ${PHP_MEMCACHED_VERSION}.tgz && cd ${PHP_MEMCACHED_VERSION}
/usr/local/php-7.0.27/bin/phpize
./configure --enable-memcached --with-php-config=/usr/local/php-7.0.27/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl && make 
make install > /tmp/memcached.txt

#add memcached.so to php.ini file

cd `cat /tmp/memcached.txt |grep "Installing shared"|awk -F: '{print $2}'`
[ -f memcached.so ] && echo "extension=memcached.so" >> /usr/local/php-7.0.27/etc/php.ini 

#restart php-fpm service

service php-fpm restart


